package registnet.records.grade.source;

import java.util.List;

import registnet.dao.CoursePeer;
import registnet.dao.GradePeer;
import registnet.records.Curriculum;
import registnet.records.DefaultCourse;
import registnet.records.Grade;
import registnet.records.Section;
import registnet.records.Student;
import registnet.records.grade.GradeSheet;
import registnet.records.source.DefaultCourseSource;
import registnet.records.source.DegreeProgramSource;

public class GradeSheetSource {

	private String courseCode;
	private String sectionName;
	private int academicYear;
	private int semester;
	private GradeSheet gradeSheet;
	
	public GradeSheetSource(String courseCode, String sectionName, int academicYear, int semester){
		this.courseCode = courseCode;
		this.sectionName = sectionName;
		this.academicYear = academicYear;
		this.semester = semester;
		this.load();
	}

	public void load(){
		GradePeer gp = new GradePeer();
		List<registnet.dao.Grade> grades = gp.getGradeBySection(new CoursePeer().getCourseId(this.courseCode), this.sectionName, this.academicYear, this.semester);
		DefaultCourse course = new DefaultCourseSource(this.courseCode).getDefaultCourse();
		Section section = new Section (course, this.sectionName);
		this.gradeSheet = new GradeSheet(section);
		
		for(int i=0; i<grades.size(); i++){
			try{
				registnet.dao.Student DAOStudent = grades.get(i).getStudent();
				Student student = new Student(DAOStudent.getStudentNumber());
				student.setSex(DAOStudent.getSex());
				student.setCurriculum(new Curriculum(DAOStudent.getCurriculum().getCurriculumCode()));
				student.getCurriculum().setDegreeProgram(new DegreeProgramSource(DAOStudent.getCurriculum().getDegreeCourse().getDegreeCode()).getDegreeProgram());
				student.setFirstName(DAOStudent.getFirstName());
				student.setMiddleName(DAOStudent.getMiddleName());
				student.setLastName(DAOStudent.getLastName());
				Grade grade = new Grade();
				grade.setGrade(grades.get(i).getGrade());
				grade.setStudent(student);
				this.gradeSheet.addGrade(grade);
			}catch(Exception e){}
		}
	}
	
	public GradeSheet getGradeSheet(){
		return this.gradeSheet;
	}
}
